home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18562 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: presby.edu!jtbell
  3. From: jtbell@presby.edu (Jon Bell)
  4. Subject: Re: How to read: operator overloaded <<
  5. Message-ID: <Dq7xwK.AK6@presby.edu>
  6. Date: Sun, 21 Apr 1996 15:33:55 GMT
  7. References: <4ldfpc$j0u@amanda.dorsai.org>
  8. Organization: Presbyterian College, Clinton, South Carolina USA
  9.  
  10.  Jeff Yu <mongoose@dorsai.org> wrote:
  11. >Hi, I have hard time to understand the syntac of << operator overloaded 
  12. >declaration. As it is stated in Lippman's C++ Primer, the << sample is
  13. >supposed to take TWO parameters: ostream& operator<<(ostream& os, String& s)
  14. >But when using it, the way is: cout<<"test"<<MyScreen<<endl;
  15. >Where is the second parameter if I consider the MyScreen as the first one?
  16.  
  17. The compiler treats binary operator expressions as if they were 
  18. two-parameter function calls.  In your case, 
  19.  
  20.   cout << "test"
  21.  
  22. is compiled as if it were
  23.  
  24.   operator<< (cout, "test")
  25.  
  26. The value returned by this function is the first parameter, cout, which 
  27. allows output operations to be "chained."  Therefore your entire output 
  28. statement is compiled as if it were
  29.  
  30.   operator<< ((operator<< ((operator<< (cout, "test")), MyScreen)), endl)
  31.  
  32. which outputs "test" and reduces to
  33.  
  34.   operator<< ((operator<< (cout, MyScreen)), endl)
  35.  
  36. which outputs MyScreen and reduces to
  37.  
  38.   operator<< (cout, endl)
  39.  
  40. which outputs the end of line.
  41.  
  42. -- 
  43. Jon Bell <jtbell@presby.edu>                        Presbyterian College
  44. Dept. of Physics and Computer Science        Clinton, South Carolina USA
  45.